import {message } from 'antd';
import { login, getMenu } from '../services/app';
export default {

  namespace: 'app',

  state: {
    theme:'dark',
    siderFold:false,
  },

  subscriptions: {
    setup({ dispatch, history }) {
      if(!localStorage.getItem('menu')){
        dispatch({
          type:'getMenu'
        })
      }
    },
  },

  effects: {
    *fetch({ payload }, { call, put }) {
      yield put({ type: 'save' });
    },
    
    *getMenu({ payload }, { call, put }) {//从数据库中获取菜单数组
      const menu = yield call(getMenu);
      if(menu){
        localStorage.setItem('menu',JSON.stringify(menu))
      }

    },
    *login({ payload }, { call, put }) {
      const data = yield call(login,payload);
      if(data==='success'){
        message.success('登录成功！',2);
        location=`${location.origin}/show`;
      } else {
        message.warn(data,2);
      }
      // const { userName, password } = payload;
      // if(userName==='admin' && password==='admin'){
      //   location=`${location.origin}/`;
      // }else {
      //   message.warn('用户或密码错误！',2);
      // }
    },
  },

  reducers: {
    save(state, action) {
      return { ...state, ...action.payload };
    },
  },

};
